<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref, computed } from 'vue'
import { scoreshowListService, scoreAddService, scoreUpdateService, scoreDeleteService, scoreshowbyidListService} from '@/api/Score1.js'
import request from '@/utils/request.js'
import { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'

// 将 scores 初始化为空数组
const scores = ref([])
// 存储输入的学生 ID
const searchStudentId = ref('')

// 计算属性，对 scores 按 toscore 从大到小排序
const sortedScores = computed(() => {
    return [...scores.value].sort((a, b) => b.toscore - a.toscore)
})

//声明异步函数，查询所有学生成绩
const scoreList = async()=>{
    let result = await scoreshowListService();
    scores.value = result.data;
}
// 页面加载时查询所有学生成绩
scoreList();

// 声明异步函数，根据学生 ID 查询成绩
const searchScoreById = async() => {
    if (!searchStudentId.value) {
        ElMessage.warning('请输入学生 ID');
        return;
    }
    try {
        let result = await scoreshowbyidListService(searchStudentId.value);
        scores.value = result.data ? [result.data] : [];
    } catch (error) {
        ElMessage.error('查询失败，请检查学生 ID');
    }
}

//控制添加分类弹窗
const dialogVisible = ref(false)

//添加分类数据模型
const scoreModel = ref({
    studentId: 0,
    grade: 0,
    xytoScore: 0,
    xycourseNum: 0,
    szbaseScore: 0,
    szhoScore: 0,
    szteScore: 4,
    kylwScore: 0,
    kyzsScore: 0,
    kysmScore: 0,
    kyjsScore: 0,
    sebaseScore: 0,
    sefjScore: 0,
    toscore: 0,
    xyscore: 0,
    sescore: 0,
    szscore: 0,
    kyscore: 0
})


//添加分类表单校验
const scoreRules = {
    studentId: [
        { required: true, message: '请输入学生 ID', trigger: 'blur' },
    ],
    grade: [
        { required: true, message: '请输入学生年级', trigger: 'blur' },
    ],
    xytoScore: [
        { required: true, message: '请输入学业总分', trigger: 'blur' },
    ],
    xycourseNum: [
        { required: true, message: '请输入所学科目数', trigger: 'blur' },
    ],
    szbaseScore: [
        { required: true, message: '请输入思政基础分', trigger: 'blur' },
        { type: 'number', max: 20, message: '思政基础分不能超过 20 分', trigger: 'blur' }
    ],
    szhoScore: [
        { required: true, message: '请输入思政荣誉分', trigger: 'blur' },
        { type: 'number', enum: [3, 4, 5], message: '思政荣誉分只能是 3 分、4 分或 5 分', trigger: 'blur' }
    ],
    szteScore: [
        { required: true, message: '请输入思政导师评分', trigger: 'blur' },
        { type: 'number', enum: [4, 5], message: '思政导师评分只能是 4 分或 5 分', trigger: 'blur' }
    ],
    kylwScore: [
        { required: true, message: '请选择科研论文等级', trigger: 'change' },
        { type: 'string', enum: ['30', '25', '20', '15', '10', '5', '0'], message: '请选择有效的论文等级', trigger: 'change' }
    ],
    kyzsScore: [
        { required: true, message: '请选择科研知识产权等级', trigger: 'change' },
        { type: 'string', enum: ['5', '3', '2', '1', '0'], message: '请选择有效的知识产权等级', trigger: 'change' }
    ],
    kysmScore: [
        { required: true, message: '请输入科研项目分', trigger: 'blur' },
    ],
    kyjsScore: [
        { required: true, message: '请输入学科竞赛分数', trigger: 'blur' },
    ],
    sebaseScore: [
        { required: true, message: '请输入社会服务基础分', trigger: 'blur' },
        { type: 'number', max: 15, message: '社会服务基础分不能超过 15 分', trigger: 'blur' }
    ],
    sefjScore: [
        { required: true, message: '请输入社会服务附加分', trigger: 'blur' },
    ]
}

//调用接口，添加表单
const addScore = async() =>{
    //调用接口
    let result = await scoreAddService(scoreModel.value)
    ElMessage.success(result.msg?result.msg:'添加成功')

    //调用分类函数
    scoreList();
    dialogVisible.value = false;
}

//定义变量，控制标题展示
const title = ref('')

//展示编辑弹窗
const showDialog = (row) =>{
    dialogVisible.value = true;
    title.value = '编辑成绩';
    //数据拷贝
    scoreModel.value.studentId = row.studentId;
    scoreModel.value.grade = row.grade;
    scoreModel.value.xytoScore = row.xytoScore;
    scoreModel.value.xycourseNum = row.xycourseNum;
    scoreModel.value.szbaseScore = row.szbaseScore;
    scoreModel.value.szhoScore = row.szhoScore;
    scoreModel.value.szteScore = row.szteScore;
    scoreModel.value.kylwScore = row.kylwScore;
    scoreModel.value.kyzsScore = row.kyzsScore;
    scoreModel.value.kysmScore = row.kysmScore;
    scoreModel.value.kyjsScore = row.kyjsScore;
    scoreModel.value.sebaseScore = row.sebaseScore;
    scoreModel.value.sefjScore = row.sefjScore;
}

//编辑成绩
const updateScore = async() =>{
    //调用接口
    let result = await scoreUpdateService(scoreModel.value)
    ElMessage.success(result.msg?result.msg:'修改成功')

    //调用分类函数
    scoreList();
    //隐藏弹窗
    dialogVisible.value = false;
}

//清空模型的数据
const clearData = () =>{
    scoreModel.value = {
        studentId: 0,
        grade: 0,
        xytoScore: 0,
        xycourseNum: 0,
        szbaseScore: 0,
        szhoScore: 0,
        szteScore: 4,
        kylwScore: 0,
        kyzsScore: 0,
        kysmScore: 0,
        kyjsScore: 0,
        sebaseScore: 0,
        sefjScore: 0
    };
}

//删除成绩
const deleteScore = async(row) =>{
    ElMessageBox.confirm(
        '你确认要删除该学生成绩信息吗？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
    .then(async() => {
        //调用接口
        let result =await scoreDeleteService(row.studentId);
        ElMessage({
            type: 'success',
            message: '删除成功',
        })
        //刷新列表
        scoreList();
    })
    .catch(() => {
        ElMessage({
            type: 'info',
            message: '取消删除',
      })
    })
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <div>
                    <el-input
                        v-model="searchStudentId"
                        placeholder="请输入学生 ID"
                        style="width: 200px; margin-right: 10px;"
                    >
                        <template #append>
                            <el-button @click="searchScoreById">查询</el-button>
                        </template>
                    </el-input>
                    <el-button @click="scoreList">显示全部</el-button>
                </div>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible = true; title='添加成绩'; clearData()">添加成绩</el-button>
                </div>
            </div>
        </template>
        <!-- 将表格数据绑定为排序后的 sortedScores -->
        <el-table :data="sortedScores" style="width: 100%">
            <el-table-column label="学生 ID" prop="studentId"> </el-table-column>
            <el-table-column label="学生年级">
                <template #default="{ row }">
                    {{ row.grade === 1 ? '研一' : row.grade === 2 ? '研二' : row.grade === 3 ? '研三' : '' }}
                </template>
            </el-table-column>
            <el-table-column label="学业总分(50)" prop="xyscore">
                <template #default="{ row }">
                    {{ row.xyscore.toFixed(1) }}
                </template>
            </el-table-column>
            <el-table-column label="思政总分(30)" prop="szscore"></el-table-column>
            <el-table-column label="社会服务总分(20)" prop="sescore"></el-table-column>
            <el-table-column label="总成绩" prop="toscore">
                <template #default="{ row }">
                    {{ row.toscore.toFixed(1) }}
                </template>
            </el-table-column>
            <!-- 奖项 -->
            <el-table-column label="奖项">
                <template #default="{ row }">
                    {{ row.temp === 1 ? '国奖' : row.temp === 2 ? '省奖' : row.temp === 3 ? '校奖' : '不获奖' }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteScore(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 添加成绩弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="60%">
            <el-form :model="scoreModel" :rules="scoreRules" label-width="150px" style="padding-right: 30px">
                <el-form-item label="学生 ID" prop="studentId">
                    <el-input-number 
                        v-model="scoreModel.studentId" 
                        :min="1" 
                        :precision="0" 
                        :step="1"
                        :max="9999999999"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="年级" prop="grade">
                    <el-select v-model="scoreModel.grade">
                        <el-option label="研一" value="1"></el-option>
                        <el-option label="研二" value="2"></el-option>
                        <el-option label="研三" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学业原始总分" prop="xytoScore">
                    <el-input-number v-model="scoreModel.xytoScore" :min="0"></el-input-number>
                </el-form-item>
                <el-form-item label="所学科目数" prop="xycourseNum">
                    <el-input-number v-model="scoreModel.xycourseNum" :min="1" :precision="0"></el-input-number>
                </el-form-item>
                <el-form-item label="思政基础分" prop="szbaseScore">
                    <el-input-number v-model="scoreModel.szbaseScore" :min="0" :max="20"></el-input-number>
                </el-form-item>
                <el-form-item label="思政荣誉分" prop="szhoScore">
                    <el-select v-model="scoreModel.szhoScore">
                        <el-option label="3 分" value="3"></el-option>
                        <el-option label="4 分" value="4"></el-option>
                        <el-option label="5 分" value="5"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="思政导师评分" prop="szteScore">
                    <el-select v-model="scoreModel.szteScore">
                        <el-option label="4 分" value="4"></el-option>
                        <el-option label="5 分" value="5"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="科研论文分" prop="kylwScore">
                    <el-select v-model="scoreModel.kylwScore" placeholder="请选择论文等级">
                        <el-option label="甲论文 30分" value="30"></el-option>
                        <el-option label="乙论文 25分" value="25"></el-option>
                        <el-option label="丙论文 20分" value="20"></el-option>
                        <el-option label="丁论文 15分" value="15"></el-option>
                        <el-option label="戊论文 10分" value="10"></el-option>
                        <el-option label="己论文 5分" value="5"></el-option>
                        <el-option label="庚论文 0分" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="科研知识产权分" prop="kyzsScore">
                    <el-select v-model="scoreModel.kyzsScore" placeholder="请选择知识产权等级">
                        <el-option label="甲级 5分" value="5"></el-option>
                        <el-option label="乙级 3分" value="3"></el-option>
                        <el-option label="丙级 2分" value="2"></el-option>
                        <el-option label="丁级 1分" value="1"></el-option>
                        <el-option label="戊级 0分" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="科研项目分" prop="kysmScore">
                    <el-input-number v-model="scoreModel.kysmScore" :min="0"></el-input-number>
                </el-form-item>
                <el-form-item label="学科竞赛分数" prop="kyjsScore">
                    <el-input-number v-model="scoreModel.kyjsScore" :min="0"></el-input-number>
                </el-form-item>
                <el-form-item label="社会服务基础分" prop="sebaseScore">
                    <el-input-number v-model="scoreModel.sebaseScore" :min="0" :max="15"></el-input-number>
                </el-form-item>
                <el-form-item label="社会服务附加分" prop="sefjScore">
                    <el-input-number v-model="scoreModel.sefjScore" :min="0"></el-input-number>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title == '添加成绩'?addScore() : updateScore()"> 确认 </el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>
